﻿using MyMusicPlay._4._0.ElementFactory;
using MyMusicPlay._4._0.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MyMusicPlay._4._0
{
    public class PlayApplication
    {
        private MainWindow MainWindow;
        /// <summary>
        /// 之前的组件[后期需要重写]
        /// </summary>
        private Dictionary<Type, BaseClass> keyValuePairs = new();
        /// <summary>
        /// 总类
        /// </summary>
        private Dictionary<Type, ManagerBase> _managerList = new();

        private static PlayApplication playApplication;

        private Dictionary<string, HookBase> Hooks = new Dictionary<string, HookBase>();

        public PlayApplication(MainWindow main)
        {
            playApplication = this;

            MainWindow = main;

            keyValuePairs.Add(typeof(Media), new Media(main));
            keyValuePairs.Add(typeof(MusicPlaying), new MusicPlaying());
            keyValuePairs.Add(typeof(Rander), new Rander(main));

            #region 组件
            keyValuePairs.Add(typeof(ViewBoard), new ViewBoard(main));
            keyValuePairs.Add(typeof(ScrollToPlaying), new ScrollToPlaying(main));
            keyValuePairs.Add(typeof(DisPlayTextAnim), new DisPlayTextAnim(main));
            keyValuePairs.Add(typeof(RightMenu), new RightMenu(main));
            keyValuePairs.Add(typeof(MyMessageBox), new MyMessageBox(main));
            keyValuePairs.Add(typeof(MusicPlayingBoard), new MusicPlayingBoard(main));
            #endregion

            keyValuePairs.Add(typeof(ElementFactoryBase), new ElementFactoryBase(main));

            keyValuePairs.Add(typeof(DispositionApp), new DispositionApp());

            Initialize();

            CreateHooks();
        }

        private void Initialize()
        {
            foreach (var item in keyValuePairs)
            {
                item.Value.Initialize();
            }

            _managerList.Add(typeof(WindowsManage), new WindowsManage());
        }

        private void CreateHooks()
        {
            Hooks.Add(typeof(MusicPlayingHook).Name, new MusicPlayingHook(MainWindow));
        }

        private void CreateManager()
        {
            //获取指定命名空间下的所有类型
            _managerList.Add(typeof(WindowsManage), new WindowsManage());
        }

        public static MainWindow GetMainWindow()
            => playApplication.MainWindow;

        public static T GetModule<T>() where T : BaseClass
            => (T)playApplication.keyValuePairs[typeof(T)];

        public static T GetManager<T>() where T : ManagerBase
            => (T)playApplication._managerList[typeof(T)];


        public List<HookBase> GetHooks()
            => Hooks.ToLists();
        public HookBase GetHook(Type type)
            => Hooks.FirstOrDefault(u => u.Key == type.Name).Value;

    }
}
